home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / unix.subproj / work.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  20.0 KB  |  792 lines

  1. /* work.c
  2.    Routines to read command files.
  3.  
  4.    Copyright (C) 1991, 1992, 1993, 1995 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char work_rcsid[] = "$Id: work.c,v 1.20 1995/06/30 21:35:14 ian Rel $";
  30. #endif
  31.  
  32. #include "uudefs.h"
  33. #include "uuconf.h"
  34. #include "system.h"
  35. #include "sysdep.h"
  36.  
  37. #include <ctype.h>
  38. #include <errno.h>
  39.  
  40. #if HAVE_OPENDIR
  41. #if HAVE_DIRENT_H
  42. #include <dirent.h>
  43. #else /* ! HAVE_DIRENT_H */
  44. #include <sys/dir.h>
  45. #define dirent direct
  46. #endif /* ! HAVE_DIRENT_H */
  47. #endif /* HAVE_OPENDIR */
  48.  
  49. /* Local functions.  */
  50.  
  51. static char *zswork_directory P((const char *zsystem));
  52. static boolean fswork_file P((const char *zsystem, const char *zfile,
  53.                   char *pbgrade));
  54. static int iswork_cmp P((constpointer pkey, constpointer pdatum));
  55.  
  56. /* These functions can support multiple actions going on at once.
  57.    This allows the UUCP package to send and receive multiple files at
  58.    the same time.  */
  59.  
  60. /* To avoid wasting a lot of time scanning the spool directory, which
  61.    might cause the remote system to time out, we limit each scan to
  62.    pick up at most a certain number of files.  */
  63. #define COMMANDS_PER_SCAN (200)
  64.  
  65. /* The ssfilename structure holds the name of a work file, as well as
  66.    its grade.  */
  67.  
  68. struct ssfilename
  69. {
  70.   char *zfile;
  71.   char bgrade;
  72.   /* Some compiler may need this, and it won't normally hurt.  */
  73.   char bdummy;
  74. };
  75.  
  76. /* The ssfile structure holds a command file name and all the lines
  77.    read in from that command file.  The union within the ssline
  78.    structure initially holds a line from the file and then holds a
  79.    pointer back to the ssfile structure; a pointer to this union is
  80.    used as a sequence pointer.  The ztemp entry of the ssline
  81.    structure holds the name of a temporary file to delete, if any.  */
  82.  
  83. #define CFILELINES (10)
  84.  
  85. struct ssline
  86. {
  87.   char *zline;
  88.   struct ssfile *qfile;
  89.   char *ztemp;
  90. };
  91.  
  92. struct ssfile
  93. {
  94.   char *zfile;
  95.   char bgrade;
  96.   /* bdummy is needed for some buggy compilers.  */
  97.   char bdummy;
  98.   int clines;
  99.   int cdid;
  100.   struct ssline aslines[CFILELINES];
  101. };
  102.  
  103. /* Static variables for the work scan.  */
  104.  
  105. static struct ssfilename *asSwork_files;
  106. static size_t cSwork_files;
  107. static size_t iSwork_file;
  108. static struct ssfile *qSwork_file;
  109.  
  110. /* Given a system name, return a directory to search for work.  */
  111.  
  112. static char *
  113. zswork_directory (zsystem)
  114.      const char *zsystem;
  115. {
  116. #if SPOOLDIR_V2
  117.   return zbufcpy (".");
  118. #endif /* SPOOLDIR_V2 */
  119. #if SPOOLDIR_BSD42 || SPOOLDIR_BSD43
  120.   return zbufcpy ("C.");
  121. #endif /* SPOOLDIR_BSD42 || SPOOLDIR_BSD43 */
  122. #if SPOOLDIR_HDB || SPOOLDIR_SVR4
  123.   return zbufcpy (zsystem);
  124. #endif /* SPOOLDIR_HDB || SPOOLDIR_SVR4 */
  125. #if SPOOLDIR_ULTRIX
  126.   return zsappend3 ("sys",
  127.             (fsultrix_has_spool (zsystem)
  128.              ? zsystem
  129.              : "DEFAULT"),
  130.             "C.");
  131. #endif /* SPOOLDIR_ULTRIX */
  132. #if SPOOLDIR_TAYLOR
  133.   return zsysdep_in_dir (zsystem, "C.");
  134. #endif /* SPOOLDIR_TAYLOR */
  135. }
  136.  
  137. /* See whether a file name from the directory returned by
  138.    zswork_directory is really a command for a particular system.
  139.    Return the command grade.  */
  140.  
  141. /*ARGSUSED*/
  142. static boolean
  143. fswork_file (zsystem, zfile, pbgrade)
  144.      const char *zsystem;
  145.      const char *zfile;
  146.      char *pbgrade;
  147. {
  148. #if SPOOLDIR_V2 || SPOOLDIR_BSD42 || SPOOLDIR_BSD43 || SPOOLDIR_ULTRIX
  149.   int cfilesys, csys;
  150.  
  151.   /* The file name should be C.ssssssgqqqq, where g is exactly one
  152.      letter and qqqq is exactly four numbers.  The system name may be
  153.      truncated to six or seven characters.  The system name of the
  154.      file must match the system name we're looking for, since there
  155.      could be work files for several systems in one directory.  */
  156.   if (zfile[0] != 'C' || zfile[1] != '.')
  157.     return FALSE;
  158.   csys = strlen (zsystem);
  159.   cfilesys = strlen (zfile) - 7;
  160.   if (csys != cfilesys
  161.       && (csys < 6 || (cfilesys != 6 && cfilesys != 7)))
  162.     return FALSE;
  163.   *pbgrade = zfile[cfilesys + 2];
  164.   return strncmp (zfile + 2, zsystem, cfilesys) == 0;
  165. #endif /* V2 || BSD42 || BSD43 || ULTRIX */
  166. #if SPOOLDIR_HDB || SPOOLDIR_SVR4
  167.   int clen;
  168.  
  169.   /* The HDB file name should be C.ssssssgqqqq where g is exactly one
  170.      letter and qqqq is exactly four numbers or letters.  We don't
  171.      check the system name, because it is guaranteed by the directory
  172.      we are looking in and some versions of uucp set it to the local
  173.      system rather than the remote one.  I'm not sure of the exact
  174.      format of the SVR4 file name, but it does not include the grade
  175.      at all.  */
  176.   if (zfile[0] != 'C' || zfile[1] != '.')
  177.     return FALSE;
  178.   clen = strlen (zfile);
  179.   if (clen < 7)
  180.     return FALSE;
  181. #if ! SPOOLDIR_SVR4
  182.   *pbgrade = zfile[clen - 5];
  183. #endif
  184.   return TRUE;
  185. #endif /* SPOOLDIR_HDB || SPOOLDIR_SVR4 */
  186. #if SPOOLDIR_TAYLOR
  187.   /* We don't keep the system name in the file name, since that
  188.      forces truncation.  Our file names are always C.gqqqq.  */
  189.   *pbgrade = zfile[2];
  190.   return (zfile[0] == 'C'
  191.       && zfile[1] == '.'
  192.       && zfile[2] != '\0');
  193. #endif /* SPOOLDIR_TAYLOR */
  194. }
  195.  
  196. /* A comparison function to look through the list of file names.  */
  197.  
  198. static int
  199. iswork_cmp (pkey, pdatum)
  200.      constpointer pkey;
  201.      constpointer pdatum;
  202. {
  203.   const struct ssfilename *qkey = (const struct ssfilename *) pkey;
  204.   const struct ssfilename *qdatum = (const struct ssfilename *) pdatum;
  205.  
  206.   return strcmp (qkey->zfile, qdatum->zfile);
  207. }
  208.  
  209. /* See whether there is any work to do for a particular system.  */
  210.  
  211. boolean
  212. fsysdep_has_work (qsys)
  213.      const struct uuconf_system *qsys;
  214. {
  215.   char *zdir;
  216.   DIR *qdir;
  217.   struct dirent *qentry;
  218. #if SPOOLDIR_SVR4
  219.   DIR *qgdir;
  220.   struct dirent *qgentry;
  221. #endif
  222.  
  223.   zdir = zswork_directory (qsys->uuconf_zname);
  224.   if (zdir == NULL)
  225.     return FALSE;
  226.   qdir = opendir ((char *) zdir);
  227.   if (qdir == NULL)
  228.     {
  229.       ubuffree (zdir);
  230.       return FALSE;
  231.     }
  232.  
  233. #if SPOOLDIR_SVR4
  234.   qgdir = qdir;
  235.   while ((qgentry = readdir (qgdir)) != NULL)
  236.     {
  237.       char *zsub;
  238.  
  239.       if (qgentry->d_name[0] == '.'
  240.       || qgentry->d_name[1] != '\0')
  241.     continue;
  242.       zsub = zsysdep_in_dir (zdir, qgentry->d_name);
  243.       qdir = opendir (zsub);
  244.       ubuffree (zsub);
  245.       if (qdir == NULL)
  246.     continue;
  247. #endif
  248.  
  249.       while ((qentry = readdir (qdir)) != NULL)
  250.     {
  251.       char bgrade;
  252.  
  253.       if (fswork_file (qsys->uuconf_zname, qentry->d_name, &bgrade))
  254.         {
  255.           closedir (qdir);
  256. #if SPOOLDIR_SVR4
  257.           closedir (qgdir);
  258. #endif
  259.           ubuffree (zdir);
  260.           return TRUE;
  261.         }
  262.     }
  263.  
  264. #if SPOOLDIR_SVR4
  265.       closedir (qdir);
  266.     }
  267.   qdir = qgdir;
  268. #endif
  269.  
  270.   closedir (qdir);
  271.   ubuffree (zdir);
  272.   return FALSE;
  273. }
  274.  
  275. /* Initialize the work scan.  We have to read all the files in the
  276.    work directory, so that we can sort them by work grade.  The bgrade
  277.    argument is the minimum grade to consider.  We don't want to return
  278.    files that we have already considered; usysdep_get_work_free will
  279.    clear the data out when we are done with the system.  This returns
  280.    FALSE on error.  */
  281.  
  282. #define CWORKFILES (10)
  283.  
  284. boolean
  285. fsysdep_get_work_init (qsys, bgrade)
  286.      const struct uuconf_system *qsys;
  287.      int bgrade;
  288. {
  289.   char *zdir;
  290.   DIR *qdir;
  291.   struct dirent *qentry;
  292.   size_t chad;
  293.   size_t callocated;
  294. #if SPOOLDIR_SVR4
  295.   DIR *qgdir;
  296.   struct dirent *qgentry;
  297. #endif
  298.  
  299.   zdir = zswork_directory (qsys->uuconf_zname);
  300.   if (zdir == NULL)
  301.     return FALSE;
  302.  
  303.   qdir = opendir (zdir);
  304.   if (qdir == NULL)
  305.     {
  306.       boolean fret;
  307.  
  308.       if (errno == ENOENT)
  309.     fret = TRUE;
  310.       else
  311.     {
  312.       ulog (LOG_ERROR, "opendir (%s): %s", zdir, strerror (errno));
  313.       fret = FALSE;
  314.     }
  315.       ubuffree (zdir);
  316.       return fret;
  317.     }
  318.  
  319.   chad = cSwork_files;
  320.   callocated = cSwork_files;
  321.  
  322.   /* Sort the files we already know about so that we can check the new
  323.      ones with bsearch.  It would be faster to use a hash table, and
  324.      the code should be probably be changed.  The sort done at the end
  325.      of this function does not suffice because it only includes the
  326.      files added last time, and does not sort the entire array.  Some
  327.      (bad) qsort implementations are very slow when given a sorted
  328.      array, which causes particularly bad effects here.  */
  329.   if (chad > 0)
  330.     qsort ((pointer) asSwork_files, chad, sizeof (struct ssfilename),
  331.        iswork_cmp);
  332.  
  333. #if SPOOLDIR_SVR4
  334.   qgdir = qdir;
  335.   while ((qgentry = readdir (qgdir)) != NULL)
  336.     {
  337.       char *zsub;
  338.  
  339.       if (qgentry->d_name[0] == '.'
  340.       || qgentry->d_name[1] != '\0'
  341.       || UUCONF_GRADE_CMP (bgrade, qgentry->d_name[0]) < 0)
  342.     continue;
  343.       zsub = zsysdep_in_dir (zdir, qgentry->d_name);
  344.       qdir = opendir (zsub);
  345.       if (qdir == NULL)
  346.     {
  347.       if (errno != ENOTDIR && errno != ENOENT)
  348.         {
  349.           ulog (LOG_ERROR, "opendir (%s): %s", zsub,
  350.             strerror (errno));
  351.           ubuffree (zsub);
  352.           return FALSE;
  353.         }
  354.       ubuffree (zsub);
  355.       continue;
  356.     }
  357.       ubuffree (zsub);
  358. #endif
  359.  
  360.       while ((qentry = readdir (qdir)) != NULL)
  361.     {
  362.       char bfilegrade;
  363.       char *zname;
  364.       struct ssfilename slook;
  365.  
  366. #if ! SPOOLDIR_SVR4
  367.       zname = zbufcpy (qentry->d_name);
  368. #else
  369.       zname = zsysdep_in_dir (qgentry->d_name, qentry->d_name);
  370.       bfilegrade = qgentry->d_name[0];
  371. #endif
  372.  
  373.       slook.zfile = zname;
  374.       if (! fswork_file (qsys->uuconf_zname, qentry->d_name,
  375.                  &bfilegrade)
  376.           || UUCONF_GRADE_CMP (bgrade, bfilegrade) < 0
  377.           || (asSwork_files != NULL
  378.           && bsearch ((pointer) &slook,
  379.                   (pointer) asSwork_files,
  380.                   chad, sizeof (struct ssfilename),
  381.                   iswork_cmp) != NULL))
  382.         ubuffree (zname);
  383.       else
  384.         {
  385.           DEBUG_MESSAGE1 (DEBUG_SPOOLDIR,
  386.                   "fsysdep_get_work_init: Found %s",
  387.                   zname);
  388.  
  389.           if (cSwork_files >= callocated)
  390.         {
  391.           callocated += CWORKFILES;
  392.           asSwork_files =
  393.             ((struct ssfilename *)
  394.              xrealloc ((pointer) asSwork_files,
  395.                    (callocated * sizeof (struct ssfilename))));
  396.         }
  397.  
  398.           asSwork_files[cSwork_files].zfile = zname;
  399.           asSwork_files[cSwork_files].bgrade = bfilegrade;
  400.           ++cSwork_files;
  401.           if (cSwork_files - chad > COMMANDS_PER_SCAN)
  402.         break;
  403.         }
  404.     }
  405.  
  406. #if SPOOLDIR_SVR4
  407.       closedir (qdir);
  408.       if (cSwork_files - chad > COMMANDS_PER_SCAN)
  409.     break;
  410.     }
  411.   qdir = qgdir;
  412. #endif
  413.  
  414.   closedir (qdir);
  415.   ubuffree (zdir);
  416.  
  417.   /* Sorting the files alphabetically will get the grades in the
  418.      right order, since all the file prefixes are the same.  */
  419.   if (cSwork_files > iSwork_file)
  420.     qsort ((pointer) (asSwork_files + iSwork_file),
  421.        cSwork_files - iSwork_file,
  422.        sizeof (struct ssfilename), iswork_cmp);
  423.  
  424.   return TRUE;
  425. }
  426.  
  427. /* Get the next work entry for a system.  This must parse the next
  428.    line in the next work file.  The type of command is set into
  429.    qcmd->bcmd If there are no more commands, qcmd->bcmd is set to 'H'.
  430.    Each field in the structure is set to point to a spot in an
  431.    malloced string.  The grade argument is never used; it has been
  432.    used by fsysdep_get_work_init.  */
  433.  
  434. /*ARGSUSED*/
  435. boolean
  436. fsysdep_get_work (qsys, bgrade, qcmd)
  437.      const struct uuconf_system *qsys;
  438.      int bgrade;
  439.      struct scmd *qcmd;
  440. {
  441.   char *zdir;
  442.  
  443.   if (qSwork_file != NULL && qSwork_file->cdid >= qSwork_file->clines)
  444.     qSwork_file = NULL;
  445.  
  446.   if (asSwork_files == NULL)
  447.     {
  448.       qcmd->bcmd = 'H';
  449.       return TRUE;
  450.     }
  451.  
  452.   zdir = NULL;
  453.  
  454.   /* This loop continues until a line is returned.  */
  455.   while (TRUE)
  456.     {
  457.       /* This loop continues until a file is opened and read in.  */
  458.       while (qSwork_file == NULL)
  459.     {
  460.       FILE *e;
  461.       struct ssfile *qfile;
  462.       int iline, callocated;
  463.       char *zline;
  464.       size_t cline;
  465.       char *zname;
  466.       char bfilegrade;
  467.  
  468.       /* Read all the lines of a command file into memory.  */
  469.       do
  470.         {
  471.           if (iSwork_file >= cSwork_files)
  472.         {
  473.           qcmd->bcmd = 'H';
  474.           ubuffree (zdir);
  475.           return TRUE;
  476.         }
  477.  
  478.           if (zdir == NULL)
  479.         {
  480.           zdir = zswork_directory (qsys->uuconf_zname);
  481.           if (zdir == NULL)
  482.             return FALSE;
  483.         }
  484.  
  485.           zname = zsysdep_in_dir (zdir, asSwork_files[iSwork_file].zfile);
  486.           bfilegrade = asSwork_files[iSwork_file].bgrade;
  487.  
  488.           ++iSwork_file;
  489.  
  490.           e = fopen (zname, "r");
  491.           if (e == NULL)
  492.         {
  493.           ulog (LOG_ERROR, "fopen (%s): %s", zname,
  494.             strerror (errno));
  495.           ubuffree (zname);
  496.         }
  497.         }
  498.       while (e == NULL);
  499.       
  500.       qfile = (struct ssfile *) xmalloc (sizeof (struct ssfile));
  501.       callocated = CFILELINES;
  502.       iline = 0;
  503.  
  504.       zline = NULL;
  505.       cline = 0;
  506.       while (getline (&zline, &cline, e) > 0)
  507.         {
  508.           if (iline >= callocated)
  509.         {
  510.           /* The sizeof (struct ssfile) includes CFILELINES
  511.              entries already, so using callocated * sizeof
  512.              (struct ssline) will give us callocated *
  513.              CFILELINES entries.  */
  514.           qfile =
  515.             ((struct ssfile *)
  516.              xrealloc ((pointer) qfile,
  517.                    (sizeof (struct ssfile) +
  518.                 (callocated * sizeof (struct ssline)))));
  519.           callocated += CFILELINES;
  520.         }
  521.           qfile->aslines[iline].zline = zbufcpy (zline);
  522.           qfile->aslines[iline].qfile = NULL;
  523.           qfile->aslines[iline].ztemp = NULL;
  524.           iline++;
  525.         }
  526.  
  527.       xfree ((pointer) zline);
  528.  
  529.       if (fclose (e) != 0)
  530.         ulog (LOG_ERROR, "fclose: %s", strerror (errno));
  531.  
  532.       if (iline == 0)
  533.         {
  534.           /* There were no lines in the file; this is a poll file,
  535.          for which we return a 'P' command.  */
  536.           qfile->aslines[0].zline = zbufcpy ("P");
  537.           qfile->aslines[0].qfile = NULL;
  538.           qfile->aslines[0].ztemp = NULL;
  539.           iline = 1;
  540.         }
  541.  
  542.       qfile->zfile = zname;
  543.       qfile->bgrade = bfilegrade;
  544.       qfile->clines = iline;
  545.       qfile->cdid = 0;
  546.       qSwork_file = qfile;
  547.     }
  548.  
  549.       /* This loop continues until all the lines from the current file
  550.      are used up, or a line is returned.  */
  551.       while (TRUE)
  552.     {
  553.       int iline;
  554.       
  555.       if (qSwork_file->cdid >= qSwork_file->clines)
  556.         {
  557.           /* We don't want to free qSwork_file here, since it must
  558.          remain until all the lines have been completed.  It
  559.          is freed in fsysdep_did_work.  */
  560.           qSwork_file = NULL;
  561.           /* Go back to the main loop which finds another file.  */
  562.           break;
  563.         }
  564.  
  565.       iline = qSwork_file->cdid;
  566.       ++qSwork_file->cdid;
  567.  
  568.       /* Now parse the line into a command.  */
  569.       if (! fparse_cmd (qSwork_file->aslines[iline].zline, qcmd))
  570.         {
  571.           ulog (LOG_ERROR, "Bad line in command file %s",
  572.             qSwork_file->zfile);
  573.           ubuffree (qSwork_file->aslines[iline].zline);
  574.           qSwork_file->aslines[iline].zline = NULL;
  575.           continue;
  576.         }
  577.       qcmd->bgrade = qSwork_file->bgrade;
  578.  
  579.       qSwork_file->aslines[iline].qfile = qSwork_file;
  580.       qcmd->pseq = (pointer) (&qSwork_file->aslines[iline]);
  581.  
  582.       if (qcmd->bcmd == 'S' || qcmd->bcmd == 'E')
  583.         {
  584.           char *zreal;
  585.  
  586.           zreal = zsysdep_spool_file_name (qsys, qcmd->ztemp,
  587.                            qcmd->pseq);
  588.           if (zreal == NULL)
  589.         {
  590.           ubuffree (qSwork_file->aslines[iline].zline);
  591.           qSwork_file->aslines[iline].zline = NULL;
  592.           ubuffree (zdir);
  593.           return FALSE;
  594.         }
  595.           qSwork_file->aslines[iline].ztemp = zreal;
  596.         }
  597.  
  598.       ubuffree (zdir);
  599.       return TRUE;
  600.     }
  601.     }
  602. }
  603.  
  604. /* When a command has been complete, fsysdep_did_work is called.  The
  605.    sequence entry was set above to be the address of an aslines
  606.    structure whose pfile entry points to the ssfile corresponding to
  607.    this file.  We can then check whether all the lines have been
  608.    completed (they will have been if the pfile entry is NULL) and
  609.    remove the file if they have been.  This means that we only remove
  610.    a command file if we manage to complete every transfer it specifies
  611.    in a single UUCP session.  I don't know if this is how regular UUCP
  612.    works.  */
  613.  
  614. boolean
  615. fsysdep_did_work (pseq)
  616.      pointer pseq;
  617. {
  618.   struct ssfile *qfile;
  619.   struct ssline *qline;
  620.   int i;
  621.   
  622.   qline = (struct ssline *) pseq;
  623.  
  624.   ubuffree (qline->zline);
  625.   qline->zline = NULL;
  626.  
  627.   qfile = qline->qfile;
  628.   qline->qfile = NULL;
  629.  
  630.   /* Remove the temporary file, if there is one.  It really doesn't
  631.      matter if this fails, and not checking the return value lets us
  632.      attempt to remove D.0 or whatever an unused temporary file is
  633.      called without complaining.  */
  634.   if (qline->ztemp != NULL)
  635.     {
  636.       (void) remove (qline->ztemp);
  637.       ubuffree (qline->ztemp);
  638.       qline->ztemp = NULL;
  639.     }
  640.  
  641.   /* If not all the lines have been returned from fsysdep_get_work,
  642.      we can't remove the file yet.  */
  643.   if (qfile->cdid < qfile->clines)
  644.     return TRUE;
  645.  
  646.   /* See whether all the commands have been completed.  */
  647.   for (i = 0; i < qfile->clines; i++)
  648.     if (qfile->aslines[i].qfile != NULL)
  649.       return TRUE;
  650.  
  651.   /* All commands have finished.  */
  652.   if (remove (qfile->zfile) != 0)
  653.     {
  654.       ulog (LOG_ERROR, "remove (%s): %s", qfile->zfile,
  655.         strerror (errno));
  656.       return FALSE;
  657.     }
  658.  
  659.   ubuffree (qfile->zfile);
  660.   xfree ((pointer) qfile);
  661.  
  662.   if (qfile == qSwork_file)
  663.     qSwork_file = NULL;
  664.  
  665.   return TRUE;
  666. }
  667.  
  668. /* Free up the results of a work scan, when we're done with this
  669.    system.  */
  670.  
  671. /*ARGSUSED*/
  672. void
  673. usysdep_get_work_free (qsys)
  674.      const struct uuconf_system *qsys;
  675. {
  676.   if (asSwork_files != NULL)
  677.     {
  678.       size_t i;
  679.  
  680.       for (i = 0; i < cSwork_files; i++)
  681.     ubuffree ((pointer) asSwork_files[i].zfile);
  682.       xfree ((pointer) asSwork_files);
  683.       asSwork_files = NULL;
  684.       cSwork_files = 0;
  685.       iSwork_file = 0;
  686.     }
  687.   if (qSwork_file != NULL)
  688.     {
  689.       int i;
  690.  
  691.       ubuffree (qSwork_file->zfile);
  692.       for (i = 0; i < qSwork_file->cdid; i++)
  693.     {
  694.       ubuffree (qSwork_file->aslines[i].zline);
  695.       ubuffree (qSwork_file->aslines[i].ztemp);
  696.     }
  697.       for (i = qSwork_file->cdid; i < qSwork_file->clines; i++)
  698.     ubuffree (qSwork_file->aslines[i].zline);
  699.       xfree ((pointer) qSwork_file);
  700.       qSwork_file = NULL;
  701.     }
  702. }
  703.  
  704. /* Save the temporary file used by a send command, and return an
  705.    informative message to mail to the requestor.  This is called when
  706.    a file transfer failed, to make sure that the potentially valuable
  707.    file is not completely lost.  */
  708.  
  709. const char *
  710. zsysdep_save_temp_file (pseq)
  711.      pointer pseq;
  712. {
  713.   struct ssline *qline = (struct ssline *) pseq;
  714.   char *zto, *zslash;
  715.   size_t cwant;
  716.   static char *zbuf;
  717.   static int cbuf;
  718.  
  719.   if (! fsysdep_file_exists (qline->ztemp))
  720.     return NULL;
  721.  
  722.   zslash = strrchr (qline->ztemp, '/');
  723.   if (zslash == NULL)
  724.     zslash = qline->ztemp;
  725.   else
  726.     ++zslash;
  727.  
  728.   zto = zbufalc (sizeof PRESERVEDIR + sizeof "/" + strlen (zslash));
  729.   sprintf (zto, "%s/%s", PRESERVEDIR, zslash);
  730.  
  731.   if (! fsysdep_move_file (qline->ztemp, zto, TRUE, FALSE, FALSE,
  732.                (const char *) NULL))
  733.     {
  734.       /* Leave the file where it was, not that is much help.  */
  735.       ubuffree (zto);
  736.       return "Could not move file to preservation directory";
  737.     }
  738.     
  739.   cwant = sizeof "File saved as\n\t/" + strlen (zSspooldir) + strlen (zto);
  740.   if (cwant > cbuf)
  741.     {
  742.       ubuffree (zbuf);
  743.       zbuf = zbufalc (cwant);
  744.       cbuf = cwant;
  745.     }
  746.  
  747.   sprintf (zbuf, "File saved as\n\t%s/%s", zSspooldir, zto);
  748.   ubuffree (zto);
  749.   return zbuf;
  750. }
  751.  
  752. /* Get the jobid of a work file.  This is needed by uustat.  */
  753.  
  754. char *
  755. zsysdep_jobid (qsys, pseq)
  756.      const struct uuconf_system *qsys;
  757.      pointer pseq;
  758. {
  759.   return zsfile_to_jobid (qsys, ((struct ssline *) pseq)->qfile->zfile,
  760.               bsgrade (pseq));
  761. }
  762.  
  763. /* Get the grade of a work file.  The pseq argument can be NULL when
  764.    this is called from zsysdep_spool_file_name, and simply means that
  765.    this is a remote file; returning -1 will cause zsfind_file to do
  766.    the right thing.  */
  767.  
  768. int
  769. bsgrade (pseq)
  770.      pointer pseq;
  771. {
  772.   const char *zfile;
  773.   char bgrade;
  774.  
  775.   if (pseq == NULL)
  776.     return -1;
  777.  
  778.   zfile = ((struct ssline *) pseq)->qfile->zfile;
  779.  
  780. #if SPOOLDIR_TAYLOR
  781.   bgrade = *(strrchr (zfile, '/') + 3);
  782. #else
  783. #if ! SPOOLDIR_SVR4
  784.   bgrade = zfile[strlen (zfile) - CSEQLEN - 1];
  785. #else
  786.   bgrade = *(strchr (zfile, '/') + 1);
  787. #endif
  788. #endif
  789.  
  790.   return bgrade;
  791. }
  792.